package com.health.service.impl;

import com.health.dao.MedicalRecordMapper;
import com.health.dao.UserMapper;
import com.health.pojo.domain.MedicalRecordDO;
import com.health.pojo.dto.MedicalRecordDTO;
import com.health.pojo.query.MedicalRecordQuery;
import com.health.service.MedicalRecordService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.health.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.health.pojo.domain.UserDO;
import com.health.constant.ResultCodeConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * MedicalRecordServiceImpl
 *
 * @author WUYU_MIN
 * @date 2025-04-29 23:51:18
 */
@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Override
    public Boolean addMedicalRecord(MedicalRecordDTO medicalRecordDTO) {
        // 验证用户是否存在
        QueryWrapper<UserDO> userQueryWrapper = Wrappers.query();
        userQueryWrapper.eq("user_id", medicalRecordDTO.getUserId());
        UserDO userDO = userMapper.selectOne(userQueryWrapper);
        if (userDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 保存医疗记录到数据库
        MedicalRecordDO medicalRecordDO = new MedicalRecordDO();
        medicalRecordDO.setUserId(medicalRecordDTO.getUserId());
        medicalRecordDO.setMedicalHistory(medicalRecordDTO.getMedicalHistory());
        medicalRecordDO.setTreatmentPlan(medicalRecordDTO.getTreatmentPlan());
        medicalRecordDO.setMedications(medicalRecordDTO.getMedications());
        medicalRecordDO.setTreatmentStart(medicalRecordDTO.getTreatmentStart());
        medicalRecordDO.setTreatmentEnd(medicalRecordDTO.getTreatmentEnd());
        int result = medicalRecordMapper.insert(medicalRecordDO);
        return result > 0;
    }

    @Override
    public Boolean deleteMedicalRecord(MedicalRecordDTO medicalRecordDTO) {
        // 验证医疗记录是否存在
        QueryWrapper<MedicalRecordDO> medicalRecordQueryWrapper = Wrappers.query();
        medicalRecordQueryWrapper.eq("medical_id", medicalRecordDTO.getMedicalId());
        MedicalRecordDO medicalRecordDO = medicalRecordMapper.selectOne(medicalRecordQueryWrapper);
        if (medicalRecordDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 删除医疗记录
        int result = medicalRecordMapper.deleteById(medicalRecordDTO.getMedicalId());
        return result > 0;
    }

    @Override
    public Boolean updateMedicalRecord(MedicalRecordDTO medicalRecordDTO) {
        // 验证医疗记录是否存在
        QueryWrapper<MedicalRecordDO> medicalRecordQueryWrapper = Wrappers.query();
        medicalRecordQueryWrapper.eq("medical_id", medicalRecordDTO.getMedicalId());
        MedicalRecordDO medicalRecordDO = medicalRecordMapper.selectOne(medicalRecordQueryWrapper);
        if (medicalRecordDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 更新医疗记录到数据库
        medicalRecordDO.setMedicalHistory(medicalRecordDTO.getMedicalHistory());
        medicalRecordDO.setTreatmentPlan(medicalRecordDTO.getTreatmentPlan());
        medicalRecordDO.setMedications(medicalRecordDTO.getMedications());
        medicalRecordDO.setTreatmentStart(medicalRecordDTO.getTreatmentStart());
        medicalRecordDO.setTreatmentEnd(medicalRecordDTO.getTreatmentEnd());
        int result = medicalRecordMapper.updateById(medicalRecordDO);
        return result > 0;
    }

    @Override
    public List<MedicalRecordDO> queryMedicalRecord() {
        return medicalRecordMapper.selectList(null);
    }

    @Override
    public List<MedicalRecordDO> queryMedicalRecordsByUserId(Integer userId) {
        // 构建查询条件
        QueryWrapper<MedicalRecordDO> medicalRecordQueryWrapper = Wrappers.query();
        medicalRecordQueryWrapper.eq("user_id", userId);
        // 查询该用户下的所有医疗记录
        return medicalRecordMapper.selectList(medicalRecordQueryWrapper);
    }
}
